package com.ThreadDemo;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: FENGYiMing
 * @Date: 2024/04/02/10:53
 * @Description:
 */
public class Thead03 {
    public static void main(String[] args) {
        GetRandomNum getRandomNum = new GetRandomNum();
        getRandomNum.start();
        KeyHandel keyHandel = new KeyHandel(getRandomNum);
        keyHandel.start();
    }
}

class GetRandomNum extends Thread {
    private boolean loop = true;

    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void run() {
        while (loop) {
            System.out.println((int) (Math.random() * 100 + 1));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class KeyHandel extends Thread {
    private Scanner scanner = new Scanner(System.in);
    private GetRandomNum a;

    public KeyHandel(GetRandomNum getRandomNum) {
        this.a = getRandomNum;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("请输入Q进行退出");
            char next = scanner.next().toUpperCase().charAt(0);
            System.out.println(next);
            if (next == 'Q') {
                a.setLoop(false);
                break;
            }

        }
    }

}
